﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for FullImageView.xaml
    /// </summary>
    public partial class FullImageView : UserControl
    {
        private double _originalSizeX = 0D;
        private double _originalSizeY = 0D;
        internal static double ZoomFactor = 0.2D;

        public static readonly DependencyProperty FileImageViewModelProperty =
            DependencyProperty.Register("FileImageViewModel", typeof(FileImageViewModel), typeof(FullImageView), new PropertyMetadata(null, OnFileImageViewModelPropertyChanged));

        public FileImageViewModel FileImageViewModel
        {
            get { return (FileImageViewModel)GetValue(FileImageViewModelProperty); }
            set { SetValue(FileImageViewModelProperty, value); }
        }

        private static void OnFileImageViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FullImageView source = d as FullImageView;

            FileImageViewModel v = e.NewValue as FileImageViewModel;
            FileImageViewModel vOld = e.OldValue as FileImageViewModel;

            source.UpdateFileImageViewModel(v, vOld);
        }

        private void UpdateFileImageViewModel(FileImageViewModel viewModel, FileImageViewModel viewModelOld)
        {
            FileImageViewModel = viewModel;

            if (viewModelOld != null)
            {
                viewModelOld.ClearFilteredImage();
                viewModelOld.DataLoaded -= viewModel_DataLoaded;
            }
            
            FileImageViewModel.ClearFilteredImage();
            SetOriginalBitmapViewModel(FileImageViewModel);
            imgMain.DataContext = FileImageViewModel;
        }

        public void DoBinding()
        {
            Binding b = new Binding();
            b.Mode = BindingMode.OneWay;
            b.Path = new PropertyPath("ImageSource");
            var binding = imgMain.SetBinding(Image.SourceProperty, b);
            imgMain.DataContext = FileImageViewModel;
            imgMain.InvalidateVisual();
        }

        public void SetOriginalBitmapViewModel(FileImageViewModel viewModel)
        {
            if (!viewModel.BitmapImageLoaded)
            {
                viewModel.DataLoaded += new EventHandler<ObjectEventArgs<string>>(viewModel_DataLoaded);
                viewModel.LoadBitmap();
            }
            else
            {
                DoBinding();
            }
        }

        void viewModel_DataLoaded(object sender, ObjectEventArgs<string> e)
        {
            FileImageViewModel viewModel = sender as FileImageViewModel;
            viewModel.DataLoaded -= viewModel_DataLoaded;
            DoBinding();
        }
        
        public FullImageView()
        {
            InitializeComponent();
        }

        internal void Zoom()
        {
            if (_originalSizeX == 0)
            {
                _originalSizeX = imgMain.ActualWidth;
                _originalSizeY = imgMain.ActualHeight;
            }
            imgMain.Width = imgMain.ActualWidth + (_originalSizeX * ZoomFactor);
            imgMain.Height = imgMain.ActualHeight + (_originalSizeY * ZoomFactor);
        }

        internal void Shrink()
        {
            if (_originalSizeX == 0)
            {
                _originalSizeX = imgMain.ActualWidth;
                _originalSizeY = imgMain.ActualHeight;
            }
            imgMain.Width = imgMain.ActualWidth - (_originalSizeX * ZoomFactor);
            imgMain.Height = imgMain.ActualHeight - (_originalSizeY * ZoomFactor);
        }
    }
}
